import VoiceNotification from './index.vue'

// 局部注册
export { VoiceNotification }

// 全局注册和管理器
const VoiceNotificationManager = {
  install(Vue, options = {}) {
    // 注册组件
    Vue.component('VoiceNotification', VoiceNotification)

    // 创建全局实例
    const VoiceNotificationConstructor = Vue.extend(VoiceNotification)
    let notificationInstance = null

    const initInstance = () => {
      notificationInstance = new VoiceNotificationConstructor({
        propsData: {
          zIndex: 2147483647, // 最大 z-index
          ...options
        }
      })
      const container = document.createElement('div')
      document.body.appendChild(container)
      notificationInstance.$mount(container)
    }

    // 显示通知
    const showNotification = (config) => {
      if (!notificationInstance) {
        initInstance()
      }
      return notificationInstance.show(config)
    }

    // 添加全局方法
    Vue.prototype.$voiceNotify = {
      // 显示通知
      show(config) {
        return showNotification(config)
      },

      // 语音播报通知
      voiceBroadcast(config) {
        return showNotification(config)
      },

      // 语音播报进度
      voiceProgress(options) {
        if (!notificationInstance) {
          initInstance()
        }
        return notificationInstance.voiceProgress(options)
      },

      // 语音播报开始
      voiceStart(message) {
        if (!notificationInstance) {
          initInstance()
        }
        return notificationInstance.voiceStart(message)
      },

      // 语音播报结束
      voiceEnd(message) {
        if (!notificationInstance) {
          initInstance()
        }
        return notificationInstance.voiceEnd(message)
      },

      // 语音播报错误
      voiceError(message) {
        if (!notificationInstance) {
          initInstance()
        }
        return notificationInstance.voiceError(message)
      },

      // 关闭指定通知
      close(id) {
        if (notificationInstance) {
          notificationInstance.close(id)
        }
      },

      // 关闭所有通知
      closeAll() {
        if (notificationInstance) {
          notificationInstance.closeAll()
        }
      },

      // 更新通知
      update(id, options) {
        if (notificationInstance) {
          notificationInstance.update(id, options)
        }
      },

      // 更新语音播报进度
      updateVoiceProgress(id, current, total, message) {
        if (notificationInstance) {
          notificationInstance.updateVoiceProgress(id, current, total, message)
        }
      },

      // 创建局部实例（用于特定全屏容器）
      createLocalInstance(container, options = {}) {
        const LocalNotificationConstructor = Vue.extend(VoiceNotification)
        const localInstance = new LocalNotificationConstructor({
          propsData: {
            attachToFullscreen: false,
            zIndex: 2147483646, // 稍低于全局实例
            ...options
          }
        })

        const localContainer = document.createElement('div')
        container.appendChild(localContainer)
        localInstance.$mount(localContainer)

        return {
          show: (config) => localInstance.show(config),
          voiceBroadcast: (config) => localInstance.voiceBroadcast(config),
          voiceProgress: (options) => localInstance.voiceProgress(options),
          voiceStart: (message) => localInstance.voiceStart(message),
          voiceEnd: (message) => localInstance.voiceEnd(message),
          voiceError: (message) => localInstance.voiceError(message),
          close: (id) => localInstance.close(id),
          closeAll: () => localInstance.closeAll(),
          update: (id, options) => localInstance.update(id, options),
          updateVoiceProgress: (id, current, total, message) =>
            localInstance.updateVoiceProgress(id, current, total, message),
          destroy: () => {
            localInstance.$destroy()
            if (localContainer.parentNode) {
              localContainer.parentNode.removeChild(localContainer)
            }
          }
        }
      }
    }
  }
}

export default VoiceNotificationManager
